package com.weixin.engine.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.weixin.common.core.doman.Result;
import com.weixin.common.dto.UserDTO;
import com.weixin.engine.models.pojo.User;
import com.weixin.engine.models.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: WangChunHui
 * @Date: 2020-04-16 22:36
 * @Description:
 */
@RestController
@RequestMapping("/user")
@DefaultProperties(defaultFallback = "globalErrorHandler")
@Slf4j
public class UserController {

    @Resource
    private UserService userService;

//    @Resource
//    private BaseFeignClient baseFeignClient;

    @Resource
    private DiscoveryClient discoveryClient;

    /**
     * 查询所有用户
     *
     * @return com.weixin.common.core.doman.Result
     * @author wangchunhui
     * @date 2022/6/20 14:47
     */
    @GetMapping
    public Result findAll() {
        List<User> userList = userService.findAll();
        return Result.SUCCESS(userList);
    }

    /**
     * 根据id查询
     *
     * @param id 主键id
     * @return com.weixin.common.core.doman.Result
     * @author wangchunhui
     * @date 2022/6/20 14:46
     */
    @GetMapping("/{id}")
    public Result findById(@PathVariable("id") Long id) {
        User user = userService.findById(id);
        return Result.SUCCESS(user);
    }

    /**
     * 添加用户
     *
     * @param user 用户
     * @return com.weixin.common.core.doman.Result
     * @author wangchunhui
     * @date 2022/6/20 15:01
     */
    @PostMapping
    public Result add(@RequestBody User user) {
        userService.add(user);
        return Result.SUCCESS();
    }

    /**
     * 修改用户
     *
     * @param user 用户
     * @return com.weixin.common.core.doman.Result
     * @author wangchunhui
     * @date 2022/6/20 15:02
     */
    @PutMapping
    public Result update(@RequestBody User user) {
        userService.update(user);
        return Result.SUCCESS();
    }

    /**
     * 根据id删除用户
     *
     * @param id 主键id
     * @return com.weixin.common.core.doman.Result
     * @author wangchunhui
     * @date 2022/6/20 15:02
     */
    @DeleteMapping("/{id}")
    public Result delById(@PathVariable("id") Long id) {
        userService.delById(id);
        return Result.SUCCESS();

    }

    @PostMapping("/selectByCondition")
    public Result selectByCondition(@RequestBody User user, @RequestParam int page, @RequestParam int size) {
        Page<User> userPage = userService.selectByCondition(user, page, size);
        return Result.SUCCESS(userPage);
    }
    public Result errorHandler(@PathVariable("id") Integer id) {
        String message = Thread.currentThread().getName() + " 系统繁忙，请稍后重试engine，o(╥﹏╥)o";
        return Result.SUCCESS(message);
    }

    /**
     * Hystrix全局测试接口
     *
     * @param id id
     * @return com.weixin.common.core.doman.Result
     * @author wangchunhui
     * @date 2022/2/17 16:36
     */
    @GetMapping("/global/error/{id}")
    @HystrixCommand
    public Result globalError(@PathVariable("id") Integer id) {
        int i = 1 / 0;
        return Result.SUCCESS();
    }

    public Result globalErrorHandler() {
        String message = Thread.currentThread().getName() + " 系统繁忙，请稍后重试global，o(╥﹏╥)o";
        return Result.SUCCESS(message);
    }

    @GetMapping("/breaker/{id}")
    public Result paymentCircuitBreaker(@PathVariable Integer id) {
        String message = userService.paymentCircuitBreaker(id);
        return Result.SUCCESS(message);
    }

    @GetMapping("/easyPoiTest")
    public void easyPoiTest(HttpServletResponse response) throws IOException {
        userService.excelExport(response);
    }

    @GetMapping("/initBinderTest")
    public String initBinderTest(@RequestParam String dateStr) {
        Map<String, Object> map = new HashMap<>();
        map.put("dateStr", dateStr);
        return map.toString();
    }

    /**
     * 根据条件查询
     *
     * @param userId  userId
     * @param userDTO 查询条件
     * @return com.weixin.common.core.doman.Result
     * @author wangchunhui
     * @date 2023/5/29 10:28
     */
    @PostMapping("/listByCondition")
    public Result listByCondition(@RequestParam(required = false) String userId, @RequestBody UserDTO userDTO) {
        log.info("userId：{}", userId);
        log.info("userDTO：{}", JSONObject.toJSONString(userDTO));
        Page<User> page = userService.pageList(userDTO);
        return Result.SUCCESS(page.getTotal(), page.getRecords());
    }
}
